﻿//using System;
//using System.Collections.Generic;
//using System.Text;
//using System.Drawing;
//using System.Runtime.InteropServices;
//using Microsoft.DirectX;
//using Microsoft.DirectX.Direct3D;
//using DirectShowLib;

//namespace MulticastTV
//{
//    public partial class d3d
//    {

//        private const int WM_GRAPHNOTIFY = 0x4000 + 88;

//        // DirectShow objects
//        private IFilterGraph2 graphBuilder;
//        private DsROTEntry rot;
//        private IBaseFilter vmr9;
//        private Allocator allocator;

//        public void InitVMR9(string filename)
//        {
//            int hr = 0;

//            // Create a DirectShow FilterGraph
//            graphBuilder = (IFilterGraph2)new FilterGraph();

//            // Add it in ROT for debug purpose
//            rot = new DsROTEntry(graphBuilder);

//            // Add a notification handler (see WndProc)
//            hr = (graphBuilder as IMediaEventEx).SetNotifyWindow(f.pictureBox1.Handle, WM_GRAPHNOTIFY, IntPtr.Zero);
//            DsError.ThrowExceptionForHR(hr);

//            // Create a VMR9 object
//            vmr9 = (IBaseFilter)new VideoMixingRenderer9();

//            IVMRFilterConfig9 filterConfig = (IVMRFilterConfig9)vmr9;

//            // We want the Renderless mode!
//            hr = filterConfig.SetRenderingMode(VMR9Mode.Renderless);
//            DsError.ThrowExceptionForHR(hr);

//            // One stream is enough for this sample
//            hr = filterConfig.SetNumberOfStreams(1);
//            DsError.ThrowExceptionForHR(hr);

//            // Create the Allocator / Presenter object
//            allocator = new Allocator(device, f);

//            IVMRSurfaceAllocatorNotify9 vmrSurfAllocNotify = (IVMRSurfaceAllocatorNotify9)vmr9;

//            // Notify the VMR9 filter about our allocator
//            hr = vmrSurfAllocNotify.AdviseSurfaceAllocator(IntPtr.Zero, allocator);
//            DsError.ThrowExceptionForHR(hr);

//            // Notify our allocator about the VMR9 filter
//            hr = allocator.AdviseNotify(vmrSurfAllocNotify);
//            DsError.ThrowExceptionForHR(hr);

//            IVMRMixerControl9 mixerControl = (IVMRMixerControl9)vmr9;

//            // Select the mixer mode : YUV or RGB
//            hr = mixerControl.SetMixingPrefs(VMR9MixerPrefs.RenderTargetYUV | VMR9MixerPrefs.NoDecimation | VMR9MixerPrefs.ARAdjustXorY | VMR9MixerPrefs.BiLinearFiltering);
//            //hr = mixerControl.SetMixingPrefs(VMR9MixerPrefs.RenderTargetRGB | VMR9MixerPrefs.NoDecimation | VMR9MixerPrefs.ARAdjustXorY | VMR9MixerPrefs.BiLinearFiltering);
//            DsError.ThrowExceptionForHR(hr);

//            // Add the filter to the graph
//            hr = graphBuilder.AddFilter(vmr9, "Video Mixing Renderer 9");
//            DsError.ThrowExceptionForHR(hr);

//            // Render the file
//            hr = graphBuilder.RenderFile(filename, null);
//            DsError.ThrowExceptionForHR(hr);

//            // Run the graph
//            hr = (graphBuilder as IMediaControl).Run();
//            DsError.ThrowExceptionForHR(hr);
//        }

//        private void shutdownDs()
//        {
//            int hr = 0;

//            // Remove the ROT entry
//            if (rot != null)
//                rot.Dispose();

//            if (graphBuilder != null)
//            {
//                // Stop DirectShow notifications
//                hr = (graphBuilder as IMediaEventEx).SetNotifyWindow(IntPtr.Zero, 0, IntPtr.Zero);

//                // Stop the graph
//                hr = (graphBuilder as IMediaControl).StopWhenReady();
//                hr = (graphBuilder as IMediaControl).Stop();

//                // Dispose the allocator
//                allocator.Dispose();

//                // Release DirectShow objects
//                Marshal.ReleaseComObject(vmr9);
//                Marshal.ReleaseComObject(graphBuilder);
//                graphBuilder = null;
//            }
//        }
//    }
//}
